<template>
	<view class="goods_list">
		<goods-list v-for="(item,index) in goodsLists" :key="index" @myclick="gotoGoodsDetail(item.goods_id)">
			<template #image>
				<image class="dq_goods_image" :src="item.goods_small_logo||defaultSrc" mode="widthFix"></image>
			</template>
			<template #title>
				{{item.goods_name}}
			</template>
			<template #price>
				{{Number(item.goods_price).toFixed(2)}}
			</template>
		</goods-list>
	</view>
</template>

<script setup>
import goodsList from "../../components/goodsList/goodsList.vue"
import {reactive, ref} from "vue"
import {onLoad,onReachBottom,onPullDownRefresh} from "@dcloudio/uni-app"
// 商品列表
const goodsLists=ref([])
// 参数
const queryObj=reactive({
	query:"",
	cid:"",
	pagenum:1,
	pagesize:10
})
// 数据总量
const total=ref(0)
// 商品图片暂无,显示默认图片
const defaultSrc=ref("https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png")
// 节流阀
const isloading=ref(false)
// 获取商品列表
const getGoodsLists=async (cb)=>{
	isloading.value=true
	const res=await uni.$http.get("/api/public/v1/goods/search",queryObj)
	if(res.data.meta.status!==200) return uni.$showMsg()
	goodsLists.value=[...goodsLists.value,...res.data.message.goods]
	total.value=res.data.message.total
	console.log(goodsLists.value);
	isloading.value=false
	cb&&cb()
}
// 跳转商品详情列表
const gotoGoodsDetail=(id)=>{
	uni.navigateTo({
		url:"/subpkg/goods_detail/goods_detail?goods_id="+id
	})
}
// 页面刷新
onLoad((options)=>{
	queryObj.query=options.query||''
	queryObj.cid=options.cid||''
	getGoodsLists()
})
// 底部触底
onReachBottom(()=>{
	if(queryObj.pagenum*queryObj.pagesize>=total.value) return uni.$showMsg("数据加载完毕!")
	if(isloading.value===true) return 
	queryObj.pagenum+=1;
	getGoodsLists()
})
// 上拉刷新
onPullDownRefresh(()=>{
	queryObj.pagenum=1
	goodsLists.value=[]
	getGoodsLists(()=>{
		uni.stopPullDownRefresh()
	})
})
</script>

<style lang="scss" scoped>
.goods_list{
	background-color: rgb(230,230,230);
	.dq_goods_image{
		width: 100%;
		height: 100%;
	}
}
</style>
